﻿using MonitorPrism_Practice.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonitorPrism_Practice.Commons
{
    public class BoundedStack<T>
    {
        private readonly LinkedList<T> _list = new LinkedList<T>();
        public readonly int _maxCapacity;

        public BoundedStack(int maxCapacity)
        {
            _maxCapacity = maxCapacity;
        }

        public int Count => _list.Count;
        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="item"></param>
        public void Push(T item)
        {
            _list.AddLast(item);

        }
        /// <summary>
        /// 出栈
        /// </summary>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public T Pop()
        {
            if (_list.Count == 0)
                throw new InvalidOperationException("栈为空");
            T item = _list.Last.Value;
            _list.RemoveLast();
            return item;
        }
        public void RemoveRange(int start, int count)
        {
            int _start = start;
            int _count = count;
            if (start < 0 || start >= _list.Count)
                _start = 0;
            if (count < start || count >= _list.Count)
                _count = _list.Count;
            LinkedListNode<T> current = _list.First;
            for (int i = 0; i < _start; i++)
                current = current.Next;
            for (int i = 0; i < _count; i++)
            {
                LinkedListNode<T> nodeToRemove = current;
                current = current.Next;
                _list.Remove(nodeToRemove);
            }
        }

        public T Peek()
        {
            if (_list.Count == 0)
                throw new InvalidOperationException("栈为空");

            return _list.Last.Value;
        }

        public void Clear() => _list.Clear();
        public IEnumerable<T> GetAll() => _list.Reverse();
    }



}
